/**
 * 
 */
package game;

import java.util.Date;

import org.fenggui.Button;
import org.fenggui.FengGUI;
import org.fenggui.TextEditor;
import org.fenggui.composite.Window;
import org.fenggui.event.ButtonPressedEvent;
import org.fenggui.event.IButtonPressedListener;
import org.fenggui.util.Point;
import org.lwjgl.input.Keyboard;
import org.lwjgl.input.Mouse;

import AGFX.F3D.F3D;
import AGFX.F3D.AppWrapper.TF3D_AppWrapper;
import AGFX.F3D.Config.TF3D_Config;

/**
 * @author Baskervil
 *
 */
public class Pexeso extends TF3D_AppWrapper
{

	TGame game;
	TMenu menu;
	TEndScreen end_screen;
	public Window  mw;
	
	public Pexeso()
	{
		
	}
	
	@Override
	public void onConfigure()
	{
		try
		{
			
			// Redefine Config
			F3D.Config = new TF3D_Config();
			F3D.Config.Load("config/pexeso.cfg");
			super.onConfigure();

		} catch (Exception e)
		{
			e.printStackTrace();
		}
	}

	
	@Override
	public void onInitialize()
	{
		//This will create the game
		this.game = new TGame();
		
		//This will create the EndScreen
		this.end_screen = new TEndScreen();
		
		//This will create the menu
		this.menu = new TMenu();
		
		this.Initialize_FengGUI();

	}
	
	
	@Override
	public void onUpdate3D()
	{
				
		// if is in game world
		if (F3D.Worlds.camera.name == "Game_Camera"){
			if (Keyboard.isKeyDown(Keyboard.KEY_N))
			{
				this.game.restartGame();
			}
			if (Keyboard.isKeyDown(Keyboard.KEY_Q))
			{
				F3D.Worlds.SetWorld("MENU");
				F3D.Worlds.SetCamera(F3D.Cameras.GetCamera("Menu_Camera"));
				F3D.Cameras.CurrentCameraID = F3D.Cameras.FindByName("Menu_Camera");
				F3D.Viewport.Clear(0f, 0f, 0f, 1f);
				this.game.destroyGame();
			}
			this.game.pickIt();
		}
		
		// if is in menu world
		if (F3D.Worlds.camera.name == "Menu_Camera"){
			this.menu.rotateModel();
			this.menu.moveCursor();
			
			if (Keyboard.isKeyDown(Keyboard.KEY_RETURN))
			{
				if (this.menu.menu_selector == 1){
					F3D.Worlds.SetWorld("GAME");
					F3D.Worlds.SetCamera(F3D.Cameras.GetCamera("Game_Camera"));
					F3D.Cameras.CurrentCameraID = F3D.Cameras.FindByName("Game_Camera");
					F3D.Viewport.Clear(0f, 0f, 0f, 1f);
				
					if (this.game.start_time == null){
						this.game.createGame(4,6);// row,column
						this.game.start_time = new Date();
						F3D.Log.info("game", "start");
					}
				}
				if (this.menu.menu_selector == 2){}
				if (this.menu.menu_selector == 3){}
				if (this.menu.menu_selector == 4){
					this.end_screen.visible = true;
					F3D.Worlds.SetWorld("THEEND");
					F3D.Worlds.SetCamera(F3D.Cameras.GetCamera("End_Camera"));
					F3D.Cameras.CurrentCameraID = F3D.Cameras.FindByName("End_Camera");
					F3D.Viewport.Clear(0f, 0f, 0f, 1f);				
					}
			}
		}
		
		//F3D.Draw.Axis(2.0f);
	}
	
	
	@Override
	public void onUpdate2D()
	{
		//F3D.Viewport.DrawInfo(0,0);
		
		if (F3D.Worlds.camera.name == "Game_Camera"){
			this.game.printTimer();
		}
		if (F3D.Worlds.camera.name == "Menu_Camera"){
			//F3D.Viewport.DrawInfo(0,0);
			if (this.menu.menu_selector == 4 & this.end_screen.visible == true){
				
				if (Mouse.isInsideWindow())
				{
		    		if (Mouse.isButtonDown(0))
		    		{
		    				this.AppTerminate = true;
		    		}
		    	}
			}
		}
		
		if (F3D.Worlds.camera.name == "End_Camera"){
			if (this.menu.menu_selector == 4 & this.end_screen.visible == true){
				
				if (Mouse.isInsideWindow())
				{
		    		if (Mouse.isButtonDown(0))
		    		{
		    				this.AppTerminate = true;
		    		}
		    	}
			}
		}
		F3D.Gui.Render();
	}
	
	public void Initialize_FengGUI()
	{

		F3D.Gui.SetTheme("data/themes/jF3D/jF3D_theme.xml");

	
		this.mw = FengGUI.createWindow(true, true, false, true);
		mw.setTitle("Multiplayer");
		mw.setSize(320, 200);
		mw.setPosition(new Point(50, 200));

		final TextEditor name = FengGUI.createWidget(TextEditor.class);
		name.setMultiline(false);
		name.setPosition(new Point(30,130));
		name.setSize(250, 29);
		this.mw.addWidget(name);
		name.setEnabled(true);		
		
		final TextEditor password = FengGUI.createWidget(TextEditor.class);
		password.setMultiline(false);
		password.setPosition(new Point(30,80));
		password.setSize(250, 29);
		this.mw.addWidget(password);
		password.setEnabled(true);		
		
		final Button button = FengGUI.createWidget(Button.class);
		button.setText("Login");
		this.mw.addWidget(button);
		
		button.addButtonPressedListener(new IButtonPressedListener()
			{
				@Override
				public void buttonPressed(ButtonPressedEvent e) {
					
					F3D.Log.info("loading", ""+name.getText());
					F3D.Log.info("loading", ""+password.getText());
				}
			}
		);
		
		
		F3D.Gui.display.addWidget(this.mw);

		
	
		
		
	}
	
	@Override
	public void OnDestroy()
	{
		
	}
	public static void main(String[] args)
	{
		new Pexeso().Execute();		
		System.exit(0); 
	}

}
